package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealExistException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 分页
     *
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //1.开启分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //2.正常查询
        List<SetmealVO> setmealList = setmealMapper.selectByNameAndCategoryIdAndStatus(setmealPageQueryDTO.getName(),setmealPageQueryDTO.getCategoryId(),setmealPageQueryDTO.getStatus());
        //3.数据转换
        PageInfo<SetmealVO> pageInfo = new PageInfo<>(setmealList);
        //PageInfo<Setmeal> pageInfo = PageInfo.of(setmealList);
        //4.封装数据
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        return pageResult;
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    public void save(SetmealDTO setmealDTO) {
        //todo 1.套餐名不能一样
        Integer countSetmealName = setmealMapper.selectByName(setmealDTO.getName());
        if(countSetmealName > 0){
            throw new SetmealExistException(MessageConstant.REPEAT_SETMEAL_NAME);
        }
        //todo 2.添加到setmeal表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setStatus(StatusConstant.DISABLE);
        //todo 3.需要主键回显,要拿到套餐id,往setmeal_dish表中添加数据
        setmealMapper.insert(setmeal);

        //todo 4.往setmeal_dish表中添加数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        insertSetmealDish(setmealDishes,setmeal);
        /*setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        setmealMapper.insertSermealAndDish(setmealDishes);*/
    }

    /**
     * 根据分类id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO queryById(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        String categoryName = setmealMapper.selectCategoryNameById(setmeal.getCategoryId());
        setmealVO.setCategoryName(categoryName);

        List<SetmealDish> setmealDishes = setmealMapper.selectListBySetmealId(id);

        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //todo 1.修改套餐表
        setmealMapper.update(setmeal);

        //todo 2.修改套餐菜单表 -- 先删后加
        // todo 2.1 根据套餐id 删除套餐菜单表
        setmealMapper.deleteSetmealDishBySetmealId(setmealDTO.getId());
        //todo 2.2 添加套餐菜单表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        insertSetmealDish(setmealDishes, setmeal);
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
       //todo 1. 起售状态不可删除
        Integer countByIdsAndStatus =  setmealMapper.selectByIdsAndStatus(ids, StatusConstant.ENABLE);
        if(countByIdsAndStatus > 0){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        //todo 2.套餐具有关联表不能删除
        Integer countBySetmealId = setmealMapper.selectCountSetmealDishBySetmealId(ids);
        if (countBySetmealId>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //todo 3.根据id批量删除套餐表
        setmealMapper.delete(ids);
        //todo 4.删除套餐菜单表
        setmealDishMapper.delete(ids);
    }

    /**
     * 修改状态
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
       Setmeal setmeal = setmealMapper.selectById(id);
       setmeal.setStatus(status);
       setmealMapper.update(setmeal);
    }

    /**
     * 根据分类id查询套餐(用户User)
     *
     * @param categoryId
     * @return {@link List}<{@link Setmeal}>
     */
    @Override
    public List<Setmeal> list(Long categoryId) {
        Setmeal setmeal = Setmeal.builder().categoryId(categoryId).status(StatusConstant.ENABLE).build();
        List<Setmeal> setmealList = setmealMapper.list(setmeal);
        return setmealList;
    }

    /**
     * 根据套餐id查询包含的菜品
     *
     * @param setmealId
     * @return {@link List}<{@link DishItemVO}>
     */
    @Override
    public List<DishItemVO> selectDishsBySetmealId(Long setmealId) {
        List<DishItemVO> dishItemVOList = setmealMapper.selectDishsBySetmealId(setmealId);
        return dishItemVOList;
    }

    /**
     * 添加套餐菜单表
     *
     * @param setmealDishes
     * @param setmeal
     */
    private void insertSetmealDish(List<SetmealDish> setmealDishes, Setmeal setmeal) {
        //List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        setmealMapper.insertSermealAndDish(setmealDishes);
    }
}
